//
//  MainTabBarController.swift
//  sinaDemo
//
//  Created by mac on 15/12/12.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit

class MainTabBarController: UITabBarController, MainTabBarDelegate {
    
    // 自定义tabbar属性
    let mainTabBar = MainTabBar()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor(red: 214.0, green: 214.0, blue: 214.0, alpha: 1.0)
        // 添加子控制器
        addControllers()

        // 设置自定义tabbar
        mainTabBar.myDelegate = self
        setValue(mainTabBar, forKey: "tabBar")
    }
    
    // 添加子控制器
    private func addControllers() {
        addchildViewControllers(HomeTableViewController(), title: "首页", imageName: "tabbar_home", selImageName: "tabbar_home_highlighted")
        addchildViewControllers(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center", selImageName: "tabbar_message_center_highlighted")
        addchildViewControllers(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover", selImageName: "tabbar_discover_highlighted")
        addchildViewControllers(ProfileTableViewController(), title: "我", imageName: "tabbar_profile", selImageName: "tabbar_profile_highlighted")
    }

    private func addchildViewControllers(vc: UIViewController, title: String, imageName: String, selImageName: String) {
        let nav = NavViewController(rootViewController: vc)
        // 设置控制器的标题
        vc.title = title
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
        // 设置控制器的tabbaritem
        vc.tabBarItem.image = UIImage(named: imageName)
//        vc.tabBarItem.selectedImage = UIImage(named: selImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        // 包装一个导航控制器
        addChildViewController(nav)
    }
    
    // mainTabBarDelegate
    func mainTabBar(tabBar: MainTabBar) {
        let composeVc = ComposeController()
        
        let nav = BaseNavController(rootViewController: composeVc)
        presentViewController(nav, animated: true, completion: nil)
        print("clickComposeButton")
    }
}
